package com.pat.web.controller;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jfinal.core.Controller;
import com.pat.web.ApiResponse;
import com.pat.web.model.Pet;
import com.pat.web.model.SickCase;

public class SickCaseController extends Controller {

	public void getList() {
		long petId = getParaToLong("petId");
		List<SickCase> sickCases = SickCase.dao
				.find("select * from sick_case where pet_id = ? order by create_time desc", petId);
		ApiResponse response = new ApiResponse();
		response.setData(sickCases);
		response.setStatusCode("0000");
		renderJson(JSON.toJSONString(response, SerializerFeature.WriteDateUseDateFormat));
	}

	public void addSickCase() {
		SickCase sickCase = getBean(SickCase.class, "");
		ApiResponse response = new ApiResponse();
		if (sickCase.getPetId() == null
				|| Pet.dao.findFirst("select id from pet where id = ?", sickCase.getPetId()) == null) {
			response.setStatusCode("2004");
			renderJson(response);
			return;
		}
		sickCase.setCreateTime(new Date()).save();
		response.setData(sickCase.getId());
		response.setStatusCode("0000");
		renderJson(response);
	}

	public void delSickCase() {
		long caseId = getParaToLong("caseId");
		SickCase.dao.deleteById(caseId);
		ApiResponse response = new ApiResponse();
		response.setStatusCode("0000");
		renderJson(response);
	}
}
